package com.seo4j.adb4j.gui;

import com.seo4j.adb4j.device.AdbBridgeManager;
import com.seo4j.adb4j.device.listener.IAndroidDevice;
import org.eknet.swing.task.AbstractTask;
import org.eknet.swing.task.Mode;
import org.eknet.swing.task.Tracker;

import java.util.List;


/**
 * Created by vps on 2016/6/23 0023.
 */
public class LoadDeviceListTask extends AbstractTask<List<IAndroidDevice>, Long> {

    AdbGUI gui = null;

    protected LoadDeviceListTask(AdbGUI gui) {
        super("Load Device Task", Mode.BACKGROUND);
        this.gui = gui;
    }

    @Override
    public List<IAndroidDevice> execute(Tracker<Long> tracker) throws Exception {


        String[] prop_keys = new String[]{
                "ro.ril.miui.imei",
                "gsm.imei.id",
                "ro.boot.serialno"
        };
        //gsm.imei.id
        //ro.ril.miui.imei
        //dhcp.wlan0.ipaddress
        //[net.hostname]: [2014813-hongmishouji]
        //ro.build.version.sdk  // api level

        //gsm.imei.id
        //ro.ril.miui.imei
        //ril.IMSI
        //dumpsys iphonesubinfo

        tracker.publish(1l);
        tracker.setPhase("Look for ");
        tracker.setProgress(0, 5, 1);
        java.util.List<IAndroidDevice> list = AdbBridgeManager.getInstance().getDevices();
        tracker.setProgress(0, 5, 5);

        return list;
        //          is = d.executeShell("dumpsys iphonesubinfo");

//                        Phone Subscriber Info:
//                        Phone Type = GSM
//                        Device ID = 867622028926494


    }

    @Override
    public void done(List<IAndroidDevice> value) {
        if (value != null && value.size() > 0) {
            AdbGUI.DEVICE_MAP.clear();

            gui.listCheckModel.clear();

            for (IAndroidDevice device : value) {
                AdbGUI.DEVICE_MAP.put(device.getSerialNo(), device);
            }



            gui.deviceTableModel.fireTableRowsInserted(value.size() - 1, value.size() - 1);
            gui.listCheckModel.addElement(value.toArray());
            gui.checkList.setModel(gui.listCheckModel);
        }
    }

    public static String substr(String source, String from, String to) {
        int start = source.indexOf(from);
        if (start != -1) {
            if (to.length() == 0) {
                return source.substring(start + from.length());
            }
            int end = source.substring(start + from.length()).indexOf(to);
            if (end != -1) {
                return source.substring(start + from.length(), start + from.length() + end);
            }

        }
        return null;
    }
}
